package com.guodongbaohe.app.myutil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

import com.guodongbaohe.app.util.DensityUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class MyBitmapUtil {

    /*看不见的view转化成bitmap*/
    public static Bitmap createBitmapOfNew(Context context, View v, int width, int height) {
        int viewWidth = DensityUtils.dip2px( context, 350 );
        //测量使得view指定大小
        int measuredWidth = View.MeasureSpec.makeMeasureSpec( viewWidth, View.MeasureSpec.EXACTLY );
        int measuredHeight = View.MeasureSpec.makeMeasureSpec( height, View.MeasureSpec.AT_MOST );
        v.measure( measuredWidth, measuredHeight );
        //调用layout方法布局后，可以得到view的尺寸大小
        v.layout( 0, 0, viewWidth, v.getMeasuredHeight() );
        Bitmap bmp = Bitmap.createBitmap( viewWidth, v.getHeight(), Bitmap.Config.RGB_565 );
        Canvas c = new Canvas( bmp );
        c.drawColor( Color.WHITE );
        v.draw( c );
        return bmp;
    }

    /*bitmap压缩*/
    public static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress( Bitmap.CompressFormat.JPEG, 100, baos );//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 700) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            image.compress( Bitmap.CompressFormat.JPEG, options, baos );//这里压缩options%，把压缩后的数据存放到baos中
            options -= 50;//每次都减少10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream( baos.toByteArray() );//把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream( isBm, null, null );//把ByteArrayInputStream数据生成图片
        return bitmap;
    }

}
